package com.cloud.wms.ui.in.fragment;

import android.os.Bundle;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.cloud.core.utils.TimeUtils;
import com.cloud.widget.EditTextDel;
import com.cloud.widget.ScanText;
import com.cloud.wms.R;
import com.cloud.wms.base.BaseViewPagerFragment;
import com.cloud.wms.ui.in.contract.InShelfContract;
import com.cloud.wms.ui.in.entity.InMarkEntity;
import com.cloud.wms.ui.in.entity.InOrderEntity;
import com.cloud.wms.ui.in.presnter.InShelfPresenter;
import com.cloud.wms.ui.main.entity.TaskEntity;
import com.cloud.wms.widget.dialog.TaskCompleteDialog;
import com.trello.rxlifecycle3.LifecycleTransformer;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @Author:Chen
 * @Date: 2021-01-24 16:59
 * @Description:
 */
public class ShelfMarkFragment extends BaseViewPagerFragment<InShelfPresenter> implements InShelfContract.View {
    @BindView(R.id.scan_text)
    ScanText scanText;
    @BindView(R.id.btn_submit)
    TextView btnSubmit;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.tv_recommend_pos)
    TextView tvRecommendPos;
    @BindView(R.id.et_shelf_pos)
    EditTextDel etShelfPos;
    private InOrderEntity inOrder;
    private BaseQuickAdapter<InMarkEntity, BaseViewHolder> mAdapter;
    private TaskEntity taskEntity;
    private String beginTime;

    @Override
    protected int getResId() {
        return R.layout.fragment_in_shelf_mark;
    }

    @Override
    protected void setupComponent() {
        getFragmentComponent().inject(this);
    }

    @Override
    protected void initEvent(Bundle bundle) {
        beginTime = TimeUtils.getCurrentDateAndTime();
        inOrder = getArguments().getParcelable("InOrder");
        taskEntity = getArguments().getParcelable("Task");

        btnSubmit.setEnabled(false);
        etShelfPos.setOnKeyListener(onKeyListenerImp);
        etShelfPos.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                btnSubmit.setEnabled(!TextUtils.isEmpty(s));
            }
        });

        rvList.setLayoutManager(new LinearLayoutManager(getContext()));
        mAdapter = new BaseQuickAdapter<InMarkEntity, BaseViewHolder>(R.layout.item_in_shelf_mark) {
            @Override
            protected void convert(BaseViewHolder helper, InMarkEntity item) {
                helper.setText(R.id.tv_mark_code, item.getBoxCode());
            }
        };
        rvList.setItemAnimator(new DefaultItemAnimator());
        rvList.setAdapter(mAdapter);

        mAdapter.setNewInstance(new ArrayList<InMarkEntity>());
    }

    @Override
    public ScanText getScanText() {
        return scanText;
    }

    @Override
    protected void onScanHandle(String result, int resultCode) {
        super.onScanHandle(result, resultCode);
        if (etShelfPos.isFocused()) {
            etShelfPos.setText(result);
            etShelfPos.setSelection(etShelfPos.getText().length());
        } else {
            scanText.addHistory(result);
            mPresenter.checkMarkCode(inOrder != null ? String.valueOf(inOrder.getInID()) :  taskEntity.getMainBillID() + "", result);
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onSuccess(Message message) {
        switch (message.what) {
            case InShelfPresenter.IN_CHECK_MARK_CODE:
                InMarkEntity markEntity = (InMarkEntity) message.obj;
                for (int i = 0; i < mAdapter.getData().size(); i++) {
                    if (markEntity.getBoxID() == mAdapter.getItem(i).getBoxID()) {
                        info("该箱唛已扫描");
                        return;
                    }
                }
                mAdapter.addData(0, markEntity);
                rvList.setVisibility(View.VISIBLE);
                btnSubmit.setEnabled(true);
                break;
            case InShelfPresenter.IN_MARK_SUBMIT:
                info("上架成功");
                mAdapter.getData().clear();
                rvList.setVisibility(View.INVISIBLE);
                btnSubmit.setEnabled(false);
                etShelfPos.setText("");
                scanText.getFocus();
                mPresenter.loadInMarkCommodityList(inOrder != null ? String.valueOf(inOrder.getInID()) :  taskEntity.getMainBillID() + "", false);
                break;
            case InShelfPresenter.CHECK_COMPLETE:
                new TaskCompleteDialog(getActivity()).show();
                break;
        }
    }

    @OnClick({R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_submit:
                mPresenter.submitMarkShelf(taskEntity, inOrder, etShelfPos.getText().toString(), mAdapter.getData(), beginTime);
                break;
        }
    }

    @Override
    public <R> LifecycleTransformer<R> toLifecycle() {
        return bindToLifecycle();
    }

    public static BaseViewPagerFragment newInstance(Bundle bundle) {
        ShelfMarkFragment fragment = new ShelfMarkFragment();
        fragment.setArguments(bundle);
        return fragment;
    }
}
